package com.lagou.sqlSession;

import com.lagou.config.XMLMapperBuilder;
import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> list = executor.executeQuery(configuration, mappedStatement, params);

        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> list = selectList(statementId, params);
        if (list.size() == 1) {
            return (T) list.get(0);
        } else {
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    @Override
    public int insertUpdateDelete(String statementId, Object... params) throws Exception {
        Executor executor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        int rows = executor.executeUpdate(configuration, mappedStatement, params);
        return rows;
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String className = method.getDeclaringClass().getName();
                String methodName = method.getName();

                String statementId = className + "." + methodName;

                String sqlType = configuration.getMappedStatementMap().get(statementId).getSqlType();

                switch (sqlType) {
                    case XMLMapperBuilder.SQLTYPE_SELECT:
                        Type genericReturnType = method.getGenericReturnType();
                    if (genericReturnType instanceof ParameterizedType) {
                            List<Object> objectList = selectList(statementId, args);
                            return objectList;
                        } else {
                            return selectOne(statementId, args);
                        }

                    case XMLMapperBuilder.SQLTYPE_INSERT:
                    case XMLMapperBuilder.SQLTYPE_UPDATE:
                    case XMLMapperBuilder.SQLTYPE_DELETE:
                        return insertUpdateDelete(statementId, args);

                    default:
                        throw new RuntimeException("配置文件解析错误");

                }

            }
        });

        return (T) proxyInstance;
    }
}
